class DSU:
    def __init__(self, n):
        self.fa = [i for i in range(n + 1)]
        self.dep = [0] * (n + 10)

    def findfa(self, x):
        if x == self.fa[x]:
            return x
        else:
            self.fa[x] = self.findfa(self.fa[x])
            return self.fa[x]

    def merge(self, x, y):
        x = self.findfa(x)
        y = self.findfa(y)
        if x != y:
            if self.dep[x] > self.dep[y]:
                self.fa[y] = x
            else:
                self.fa[x] = y
                if self.dep[x] == self.dep[y]:
                    self.dep[y] += 1


n, m = map(int, input().split())
edge = [tuple(map(int, input().split())) for _ in range(m)]
edge.sort(key=lambda x: x[2])
dsu = DSU(n)
ans = 0
for e in edge:
    u = e[0]
    v = e[1]
    if dsu.findfa(u) != dsu.findfa(v):
        dsu.merge(u, v)
        ans += e[2]
print(ans)
